/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author johnkim
 *
 */
public class DayveItemDatabaseHandler {

	public static final String TAG = DayveItemDatabaseHandler.class.getSimpleName();
	
	Context ctx;
	DayveItemDatabaseHelper helper;
	SQLiteDatabase db;
	
	public DayveItemDatabaseHandler(Context ctx) {
		this.ctx = ctx;
		
		helper = new DayveItemDatabaseHelper(ctx);		
		
		db = helper.getWritableDatabase();
	}
	
	/**
	 * Database open 
	 */	
    public static DayveItemDatabaseHandler open(Context ctx) {
    	return new DayveItemDatabaseHandler(ctx);
    }
    
    /**
     * Transaction 을 정의 한다.
     */
	public void beginTransaction() {db.beginTransaction();}
	
	public void setTransactionSuccessful() {db.setTransactionSuccessful();}
	
	public void endTransaction() {db.endTransaction();}

	public SQLiteDatabase getSQLiteDatabase() {
		return this.db;
	}
	
	/**
	 * Database close 
	 * */
	public void close() {
		if(db.isOpen()) db.close();
	}

	// Item 테이블 컬럼 목록.
	private static final String[] DAYVE_ITEM = new String[] {
		"_id", 
		"id_myhistory_items",
		"name",
		"comment", 
		"amount", 
		"open_item_yn",
		"favorite_yn",
		"activate_yn",
		"modify_date",
		"register_date",
		"myhistory_members_id", 
		"myhistory_categories_id_myhistory_categories"
	};
	
	/**
	 * 아이템 목록을 가져 온다.
	 * 
	 * @param _id
	 * @param limit
	 * @param DAYVE_LIST_SORT : 정열 기준.(SORT_BY_CHARACTER/SORT_BY_DATE)
	 * @return
	 */
	public ArrayList<DayveItem> selectItemList(String _id, String limit, String DAYVE_LIST_SORT) {
		Log.i(TAG, "selectItemList");
		
		String SORT_BY = " _id ASC ";
		
		if(("SORT_BY_CHARACTER").equals(DAYVE_LIST_SORT)) {
			SORT_BY = " _id ASC ";
		} else if(("SORT_BY_DATE").equals(DAYVE_LIST_SORT)) {
			SORT_BY = " modify_date DESC ";
		}
		
		ArrayList<DayveItem> itemList = new ArrayList<DayveItem>();
		
		Cursor cursor = db.query(
				"dayve_item", 
				DAYVE_ITEM, 
				"_id>?", 
				new String[] {_id}, 
				null, 
				null, 
				SORT_BY, 
				limit
				);
		
		DayveItem dayveItem = null;
		
		while(cursor.moveToNext()) {
			dayveItem = new DayveItem();
			
			dayveItem.set_id(cursor.getString(0));
			dayveItem.setIdMyHistoryItems(cursor.getString(1));
			dayveItem.setName(cursor.getString(2));
			dayveItem.setComment(cursor.getString(3));
			dayveItem.setAmount(cursor.getString(4));
			dayveItem.setOpenItemYn(cursor.getString(5));
			dayveItem.setFavoriteYn(cursor.getString(6));
			dayveItem.setActivateYn(cursor.getString(7));
			dayveItem.setModifyDate(cursor.getString(8));
			dayveItem.setRegisterDate(cursor.getString(9));
			dayveItem.setMyHistoryMembersId(cursor.getString(10));
			dayveItem.setMyHistoryCategoriesIdMyHistoryCategories(cursor.getString(11));
			
			itemList.add(dayveItem);
		}
		
		cursor.close();
		
		return itemList;
	}
	
	/**
	 * 해당 category 의 item 목록을 가져 온다.
	 * 
	 * @param categoryId
	 * @return
	 */
	public ArrayList<DayveItem> selectItemListForCategory(String categoryId) {
		Log.i(TAG, "selectItemListForCategory");
		
		ArrayList<DayveItem> itemList = new ArrayList<DayveItem>();
		
		Cursor cursor = db.query(
				"dayve_item", 
				DAYVE_ITEM, 
				"myhistory_categories_id_myhistory_categories=?", 
				new String[] {categoryId}, 
				null, 
				null, 
				" modify_date DESC ", 
				null
				);
		
		DayveItem dayveItem = null;
		
		while(cursor.moveToNext()) {
			dayveItem = new DayveItem();
			
			dayveItem.set_id(cursor.getString(0));
			dayveItem.setIdMyHistoryItems(cursor.getString(1));
			dayveItem.setName(cursor.getString(2));
			dayveItem.setComment(cursor.getString(3));
			dayveItem.setAmount(cursor.getString(4));
			dayveItem.setOpenItemYn(cursor.getString(5));
			dayveItem.setFavoriteYn(cursor.getString(6));
			dayveItem.setActivateYn(cursor.getString(7));
			dayveItem.setModifyDate(cursor.getString(8));
			dayveItem.setRegisterDate(cursor.getString(9));
			dayveItem.setMyHistoryMembersId(cursor.getString(10));
			dayveItem.setMyHistoryCategoriesIdMyHistoryCategories(cursor.getString(11));
			
			itemList.add(dayveItem);
		}
		
		cursor.close();
		
		return itemList;
	}
	
	/**
	 * 아이템을 저장 한다.
	 * 
	 * @param values
	 * @return
	 */
	public Long insertDayveItem(ContentValues values) {
//		Log.i(TAG, "insertDayveItem");
		return db.insert("dayve_item", "", values);
	}
	
	/**
	 * 아이템을 삭제 한다.
	 * 
	 * @return
	 */
	public int deleteDayveItem() {
		Log.i(TAG, "deleteDayveItem");
		return db.delete("dayve_item", null, null);
	}
}
